import os
import utils
import shutil
from tqdm import tqdm


path_src = '/media/fang/TOSHIBA EXT/Temp/Rect/Big'
path_dst = '/media/fang/TOSHIBA EXT/Temp/Rect/BigMerge'

for root, dirs, files in os.walk(path_src):
    if os.path.exists(root.replace(path_src, path_dst)) is False:
        os.makedirs(root.replace(path_src, path_dst))
    for file in tqdm(files):
        if file.split(".")[-1] == 'jpg':
            if os.path.exists(root.replace(path_src, path_dst) + '/' + file) is False:
                shutil.copy(root + '/' + file, root.replace(path_src, path_dst) + '/' + file)
        elif file.split(".")[-1] == 'xml':
            infos = utils.ReadXml(root + '/' + file)
            xmin = min([x[0] for x in infos])
            ymin = min([x[1] for x in infos])
            xmax = max([x[2] for x in infos])
            ymax = max([x[3] for x in infos])
            label = file.split(".")[0].split("_")[-1]
            if "RemoveLimit" in label:
                label = label.replace("RemoveLimit", "")
            if "-" in label:
                label = label.replace("-", "")
            new_info = [root.replace(path_src, path_dst) + '/' + file]
            new_info.append([xmin, ymin, xmax, ymax, label])
            utils.WriteXml(new_info, 300, 300)